package cc.ilzy.mall.coupon.service.impl;

import cc.ilzy.mall.enums.CouponCategoryEnum;
import cc.ilzy.mall.enums.CouponPulishEnum;
import cc.ilzy.mall.model.pojo.Coupon;
import cc.ilzy.mall.coupon.mappers.CouponMapper;
import cc.ilzy.mall.coupon.service.ICouponService;
import cc.ilzy.mall.model.vo.CouponVO;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ilzy
 * @since 2025-07-06
 */
@Service
public class CouponServiceImpl extends ServiceImpl<CouponMapper, Coupon> implements ICouponService {

    @Override
    public IPage<CouponVO> queryCouponPage(Integer pageNum, Integer pageSize, Long couponId, String category) {
        IPage<Coupon> page = new Page<>(pageNum, pageSize);
        lambdaQuery().eq(Coupon::getPublish, CouponPulishEnum.PUBLISH)
                     .eq(couponId != null, Coupon::getId, couponId)
                     .eq(Coupon::getCategory, category != null ? category : CouponCategoryEnum.PROMOTION.name())
                     .orderByDesc(Coupon::getCreateTime)
                     .page(page);
        // 将 Coupon 转换为 CouponVO
        return page.convert(coupon -> BeanUtil.copyProperties(coupon, CouponVO.class));
    }
}
